﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WucCloud.Database;

namespace WucCloud.Application
{
    /// <summary>
    /// 泛型服务 实现
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class BaseService<TEntity> where TEntity : class,new()
    {
        /// <summary>
        /// 用于当前表操作（通过在子类的构造函数中注入，此处无需构造）
        /// </summary>
        protected IBaseRepository<TEntity> _repository;

        /// <summary>
        /// 用于其他表操作（事务操作）
        /// </summary>
        protected IUnitOfWork _unitOfWork;

        #region Basic 基础方法
        /// <summary>
        /// 带过滤的查询
        /// </summary>
        /// <returns></returns>
        public ISugarQueryable<TEntity> SugarQueryable()
        {
            return _repository.SugarQueryable();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public IInsertable<TEntity> Insertable(TEntity entity)
        {
            return _repository.Insertable(entity);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns></returns>
        public IInsertable<TEntity> Insertable(List<TEntity> entities)
        {
            return _repository.Insertable(entities);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public IUpdateable<TEntity> Updateable(TEntity entity)
        {
            return _repository.Updateable(entity);
        }

        /// <summary>
        /// 批量编辑
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns></returns>
        public IUpdateable<TEntity> Updateable(List<TEntity> entities)
        {
            return _repository.Updateable(entities);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public IDeleteable<TEntity> Deleteable()
        {
            return _repository.Deleteable();
        }
        #endregion

        #region Insert 新增方法
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>是否新增成功</returns>
        public async Task<bool> InsertAsync(TEntity entity)
        {
            return await _repository.InsertAsync(entity);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns>是否新增成功</returns>
        public async Task<bool> InsertAsync(List<TEntity> entities)
        {
            return await _repository.InsertAsync(entities);
        }
        #endregion

        #region Update 编辑方法
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>是否编辑成功</returns>
        public async Task<bool> UpdateAsync(TEntity entity)
        {
            return await _repository.UpdateAsync(entity);
        }

        /// <summary>
        /// 批量编辑
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns>是否编辑成功</returns>
        public async Task<bool> UpdateAsync(List<TEntity> entities)
        {
            return await _repository.UpdateAsync(entities);
        }
        #endregion

        #region Delete 删除方法
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(string keyId)
        {
            return await _repository.DeleteAsync(keyId);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(TEntity entity)
        {
            return await _repository.DeleteAsync(entity);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">实体对象集合</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(TEntity[] entities)
        {
            return await _repository.DeleteAsync(entities);
        }
        #endregion

        #region Query 查询方法
        /// <summary>
        /// 检验数据是否存在
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _repository.AnyAsync(expression);
        }

        /// <summary>
        /// 检查信息总条数
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        public async Task<int> CountAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _repository.CountAsync(expression);
        }

        /// <summary>
        /// 通过Id查询
        /// </summary>
        /// <param name="keyId">主键Id</param>
        /// <returns></returns>
        public async Task<TEntity> FindByIdAsync(string keyId)
        {
            return await _repository.FindByIdAsync(keyId);
        }

        /// <summary>
        /// 自定义条件查询（单条）
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _repository.FindAsync(expression);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryableAsync()
        {
            return await _repository.QueryableAsync();
        }

        /// <summary>
        /// 自定义条件查询（多条）
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryableAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _repository.QueryableAsync(expression);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">分页组件类</param>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryableAsync(PageModel page)
        {
            return await _repository.QueryableAsync(page);
        }

        /// <summary>
        /// 自定义条件分页查询
        /// </summary>
        /// <param name="expression">Lambda表达式（查询条件）</param>
        /// <param name="page">分页组件类</param>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryableAsync(Expression<Func<TEntity, bool>> expression, PageModel page)
        {
            return await _repository.QueryableAsync(expression, page);
        }
        #endregion
    }
}
